from direct.gui.DirectGui import DirectFrame, DirectEntry
from Cube.gui.QScrolledFrame import QScrolledTextLabelFrame
from Cube.gui.QScrolledFrame import QScrolledEntryFrame
from Cube.gui.QSizer import ResizedCallBackSet, BoxSizer
from Cube.gui.DirectGUIPropertiesAccess import AlignText

class ChatRoomFrame(DirectFrame):

    def __init__(self, parent = None, param_textboard = {}, param_textInputInfo = {}):
        DirectFrame.__init__(self, parent)

        self.textBoard = QScrolledTextLabelFrame(parent = self,
                                                 param_Label = param_textboard["Label"],
                                                 param_ScrolledFrame = param_textboard["ScrolledFrame"],
                                                 )

        if not param_textInputInfo["Entry"].has_key("command"):
            param_textInputInfo["Entry"]["command"] = self.MessageEnter
        self.textInput = QScrolledEntryFrame(parent = self,
                                             param_Entry = param_textInputInfo["Entry"],
                                             param_ScrolledFrame = param_textInputInfo["ScrolledFrame"],
                                             )

        sizerMain = BoxSizer(BoxSizer.VERTICAL, gap = 0.01)
        sizerMain.Add(self.textBoard, 1)
        sizerMain.Add(self.textInput, 0)
        sizerMain.Attach2Frame(self)

    def destroy(self):
        # remove all event bind
        self.ignoreAll()
        DirectFrame.destroy(self)

    def ChatEntryCallBackSet(self, fn):
        self.textInput.EntryCallBackSet(fn)

    def MessageEnter(self,text):
        if text:
            self.TextEnterHandle(text)
            self.textInput.enterText('')

    # export function ------------------------------
    # output
    # override it
    def TextEnterHandle(self, text):
        #pass
        self.appendMessage(text)

    # input
    # write text on chat room's text board
    def appendMessage(self,text):
        self.textBoard.appendText(text)

    def clearMessage(self):
        self.textBoard.clearText()

    def InputEntryClear(self):
        self.textInput.EntryClear()
